package com.tandashi.springbootstudy01.sensor.receiver.impl;

import com.tandashi.springbootstudy01.convention.constants.IntegerConst;
import com.tandashi.springbootstudy01.domain.activity.Opening2023;
import com.tandashi.springbootstudy01.sensor.receiver.ActivityReceiver;
import com.tandashi.springbootstudy01.service.Opening2023Service;
import com.tandashi.springbootstudy01.util.Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import static com.tandashi.springbootstudy01.convention.constants.Opening2023Const.*;

@Component
@Slf4j
public class Opening2023Reciver extends ActivityReceiver<Opening2023> {
    @Override
    protected void receiveAction(Opening2023 message) {
        /*if (StringUtils.isNoneBlank(message.getUnionId())
                && message.getOperation() >= OPEN_NOTE
                && message.getOperation() <= OPEN_PLAN) {
            // sendToBi(message);
        }*/
        log(message);
        if (StringUtils.isNoneBlank(message.getHolderId())) {
            handleTarget(message);
        }
    }

    private void handleTarget(Opening2023 message) {
        write2db(message);
    }

    private void write2db(Opening2023 message) {
        try {
            if (!completeAction(message)) {
                return;
            }

            Opening2023 act = opening2023Service.selectOne(message.getAgentCode(), message.getHolderId());
            fillCustInfo(message);

            if (null == act) {
                try {
                    opening2023Service.insert(message);
                } catch (DuplicateKeyException e) {
                    opening2023Service.update(message);
                }
            } else {
                opening2023Service.update(message);
            }
        } catch (Exception e) {
            log.error("write2db error", e);
        }
    }

    private void fillCustInfo(Opening2023 message) {
        //TODO es query
        return;
    }

    private boolean completeAction(Opening2023 msg) {
        msg.setIsShared(null);
        msg.setIsOpenNote(null);
        msg.setIsOpenPlan(null);
        msg.setIsQuit(null);
        msg.setIsIntention(null);

        msg.setSharedTime(null);
        msg.setOpenNoteTime(null);
        msg.setOpenPlanTime(null);
        msg.setQuitTime(null);
        msg.setIntentionTime(null);

        switch (msg.getOperation()) {
            case SHARED:
                msg.setIsShared(IntegerConst.TRUE);
                msg.setSharedTime(Utils.forLocalDateTime(msg.getTs()));
                return true;
            case OPEN_NOTE:
                msg.setIsOpenNote(IntegerConst.TRUE);
                msg.setOpenNoteTime(Utils.forLocalDateTime(msg.getTs()));
                return true;
            case OPEN_PLAN:
                msg.setIsOpenPlan(IntegerConst.TRUE);
                msg.setOpenPlanTime(Utils.forLocalDateTime(msg.getTs()));
                return true;
            case QUIT:
                msg.setIsQuit(IntegerConst.TRUE);
                msg.setQuitTime(Utils.forLocalDateTime(msg.getTs()));
                msg.setIsIntention(IntegerConst.FALSE);
                return true;
            case INTENTION:
                msg.setIsIntention(IntegerConst.TRUE);
                msg.setIntentionTime(Utils.forLocalDateTime(msg.getTs()));
                msg.setIsQuit(IntegerConst.NO);
                return true;
            default:
                return false;
        }
    }

    private void log(Object obj) {
        try {
            if (log.isInfoEnabled()) {
                log.info("msg:{}", objectMapper.writeValueAsString(obj));
            }
        } catch (Exception ignore) {
        }
    }

    @Override
    protected boolean validate(Opening2023 message) {
        return StringUtils.isNotBlank(message.getAgentCode());
    }

    @Override
    public String topic() {
        return "act-month04";
    }

    @Autowired
    private Opening2023Service opening2023Service;
}
